/*
 * Copyright (c) 2021.版权所有高金磊
 */

package 领扣算法.A简单题.验证外星语词典;


import java.util.HashMap;

public class Main {
    HashMap<Character,Character> middle;
    public boolean isAlienSorted(String[] words, String order) {
        middle = new HashMap<>();
        char i=0;
        for (char c : order.toCharArray()) {
            middle.put(c, i);
            i++;
        }
        for (int j = 0; j < words.length - 1; j++) {
            if (!check(words[j],words[j+1]))
                return false;
        }
        return true;
    }

    private boolean check(String word, String word1) {
        StringBuilder stringBuilder=new StringBuilder();
        for (char c : word.toCharArray()) {
            stringBuilder.append(middle.get(c));
        }
        StringBuilder stringBuilder1=new StringBuilder();
        for (char c : word1.toCharArray()) {
            stringBuilder1.append(middle.get(c));
        }
        return stringBuilder.toString().compareTo(stringBuilder1.toString())<=0;
    }
}
